package unit9_10;

import java.sql.*;

public class JDBC_test1 {

    private static Connection conn;

    public static void main(String[] args) {
        try {
//            加载数据库驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

//            连接数据库
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/school?useSSL=true&characterEncoding=utf-8&user=root&password=123456");
            System.out.println("连接成功！");

            Statement statement=conn.createStatement();

//            编写数据库
            String sql;
            sql="select * from stu";
            ResultSet ret=statement.executeQuery(sql);
            while(ret.next()){
                Integer sno=ret.getInt("sno");
                String name=ret.getString("sname");
                String sex =ret.getString("ssex");

                System.out.print("no:"+sno);
                System.out.print(",name:"+name);
                System.out.println(",sex:"+sex);
            }
            statement.close();

        } catch (Exception e) {
            System.out.println("连接失败！");
            e.printStackTrace();
        }finally {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

}
